package org.atovio.coolexam.controller;

import org.atovio.coolexam.common.Result;
import org.atovio.coolexam.entity.*;
import org.atovio.coolexam.service.*;
import org.atovio.coolexam.viewmodel.admin.answerpaper.AnswerPaperItemVM;
import org.atovio.coolexam.viewmodel.admin.answerpaper.TableRowVm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

@RestController
@RequestMapping("/answerPapers")
public class AnswerPaperController {

    private final UserService userService;
    private final QuestionService questionService;
    private final ExamPaperService examPaperService;
    private final QuestionOptionsService questionOptionsService;
    private final ExamPaperAnswerService examPaperAnswerService;
    private final ExamPaperQuestionCustomerAnswerService examPaperQuestionCustomerAnswerService;

    @Autowired
    public AnswerPaperController(ExamPaperService examPaperService, ExamPaperAnswerService examPaperAnswerService,
                                 UserService userService, QuestionService questionService, QuestionOptionsService questionOptionsService,
                                 ExamPaperQuestionCustomerAnswerService examPaperQuestionCustomerAnswerService) {
        this.userService = userService;
        this.questionService = questionService;
        this.examPaperService = examPaperService;
        this.questionOptionsService = questionOptionsService;
        this.examPaperAnswerService = examPaperAnswerService;
        this.examPaperQuestionCustomerAnswerService = examPaperQuestionCustomerAnswerService;
    }

    @PutMapping("status/{id}/{status}")
    public Result modifyAnswerPaperStatus(@PathVariable Integer id, @PathVariable Integer status,
                                          @RequestBody List<ExamPaperQuestionCustomerAnswer> answers) {
        AtomicInteger customerScore = new AtomicInteger();
        answers.forEach(a -> {
            customerScore.addAndGet(a.getCustomerScore());
            examPaperQuestionCustomerAnswerService.modifyCustomerScoreById(a.getId(), a.getCustomerScore());
        });
        examPaperAnswerService.modifyUserScoreById(id, customerScore.get());
        examPaperAnswerService.modifyStatusById(id, status);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getAnswerPaperItems(@PathVariable Integer id) {
        List<AnswerPaperItemVM> itemVMs = new ArrayList<>();
        List<ExamPaperQuestionCustomerAnswer> answerItems = examPaperQuestionCustomerAnswerService.getSelective(
                Map.of("examPaperAnswerId", id));
        answerItems.forEach(ats -> {
            AnswerPaperItemVM vm = new AnswerPaperItemVM();
            Question q = questionService.getById(ats.getQuestionId());
            List<QuestionOptions> qos = questionOptionsService.getByQuestionId(q.getId());

            vm.setQuestion(q);
            vm.setQuestionOptions(qos);
            vm.setQuestionCustomerAnswer(ats);

            itemVMs.add(vm);
        });

        return Result.success(itemVMs);
    }

    @GetMapping("/user")
    public Result getAnswerPaperByStatusAndUser(@RequestParam Integer status, @RequestParam Integer userId) {
        List<TableRowVm> tableData = new ArrayList<>();
        List<ExamPaperAnswer> answerPapers = examPaperAnswerService.getBySelective(
                Map.of("status", status, "createUserId", userId));

        answerPapers.forEach(ap -> {
            TableRowVm vm = new TableRowVm();
            vm.setExamPaperAnswer(ap);
            vm.setExamPaper(examPaperService.getById(ap.getExamPaperId()));
            vm.setUser(userService.getByUserId(ap.getCreateUserId()));

            tableData.add(vm);
        });

        return Result.success(tableData);
    }

    @GetMapping
    public Result getAnswerPaperByStatus(@RequestParam Integer status) {
        List<TableRowVm> tableData = new ArrayList<>();
        List<ExamPaperAnswer> answerPapers = examPaperAnswerService.getByStatus(status);

        answerPapers.forEach(ap -> {
            TableRowVm vm = new TableRowVm();
            vm.setExamPaperAnswer(ap);
            vm.setExamPaper(examPaperService.getById(ap.getExamPaperId()));
            vm.setUser(userService.getByUserId(ap.getCreateUserId()));

            tableData.add(vm);
        });

        return Result.success(tableData);
    }
}
